package lanou.giftsay_01.main.hotmain;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.sharesdk.framework.ShareSDK;
import lanou.giftsay_01.tools.GiftUser;
import lanou.giftsay_01.R;
import lanou.giftsay_01.tools.NewBean;
import lanou.giftsay_01.cn.sharesdk.onekeyshare.OnekeyShare;
import lanou.giftsay_01.main.LogInActivity;
import lanou.giftsay_01.tools.BaseActivity;
import lanou.giftsay_01.tools.DBTool;



/**
 * Created by dllo on 16/8/20.
 */
public class HotSecondActivity extends BaseActivity implements  View.OnClickListener {

    private WebView view;
    private String name;
    private String price;
    private String ivUrl;
    private String counts;
    private DBTool dbTool;
    private NewBean collectionBean;
    private SharedPreferences sharedPreferences ;
    private ImageView share;
    private ImageView favorite;
    boolean  click ;

    @Override
    protected int setLayout() {
        return R.layout.activity_hot;
    }

    @Override
    protected void initView() {

        String name1 = "lanou.giftsay_01.SETTING";
        sharedPreferences = getSharedPreferences(name1 , MODE_PRIVATE);
        dbTool = DBTool.getInstance();
        view = (WebView) findViewById(R.id.hot_webView);
        favorite = (ImageView) findViewById(R.id.hreat_iv);
        favorite.setOnClickListener(this);
        share = (ImageView) findViewById(R.id.share_hot);

    }

    @Override
    protected void initDate() {
        Intent intent = getIntent();
        String url = intent.getStringExtra("key");
        name = intent.getStringExtra("name");
        price = intent.getStringExtra("price");
        ivUrl = intent.getStringExtra("url");
        counts = intent.getStringExtra("counts");
        GiftUser giftUser =BmobUser.getCurrentUser(GiftUser.class);
        if (giftUser!= null){
            dbTool.queryTwoCollection("nickName = ?" , "name = ?" , giftUser.getUsername() , name , new DBTool.QueryListener() {
                @Override
                public void onQueryComplete(ArrayList<NewBean> arrayList) {
                    Log.d("HotSecondActivity", "arrayList.size():" + arrayList.size());
                    if (arrayList.size()!= 0){
                        favorite.setImageResource(R.mipmap.ic_action_compact_favourite_selected);
                        click = true ;
                    }
                }
            });
        }

        view.loadUrl(url);
        WebSettings settings = view.getSettings();
        settings.setJavaScriptEnabled(true);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();

    }
    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }

    @Override
    public void onClick(View v) {

        if (click){
            favorite.setImageResource(R.mipmap.hreat);
            GiftUser giftUser = BmobUser.getCurrentUser(GiftUser.class);
            dbTool.deleteTowColletionBean(" name = ?" , " nickName = ?" , name , giftUser.getUsername());
            BmobQuery<NewBean> query = new BmobQuery<NewBean>();
            query.addWhereEqualTo("nickName",giftUser.getUsername());
            query.addWhereEqualTo("name" , name);
            query.setLimit(50);
            query.findObjects(new FindListener<NewBean>() {
                @Override
                public void done(List<NewBean> list, BmobException e) {
               if (e == null){
                   Toast.makeText(HotSecondActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                   for(NewBean newBean : list){
                       Log.d("HotSecondActivity", newBean.getObjectId());
                       newBean.setObjectId(newBean.getObjectId());
                       newBean.delete(new UpdateListener() {
                       @Override
                       public void done(BmobException e) {
                       if (e == null){
                           Toast.makeText(HotSecondActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                       }else {
                           Toast.makeText(HotSecondActivity.this, "更新失败", Toast.LENGTH_SHORT).show();
                       }

                       }
                   }) ;

                   }
               }else {
                   Toast.makeText(HotSecondActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
               }
                }
            });
            click = false ;
            Toast.makeText(this, "喜新厌旧的玩意儿", Toast.LENGTH_SHORT).show();
        }else {
            GiftUser giftUser = BmobUser.getCurrentUser(GiftUser.class);
            if (giftUser != null) {
                favorite.setImageResource(R.mipmap.ic_action_compact_favourite_selected);
                Toast.makeText(this, "喜欢成功", Toast.LENGTH_SHORT).show();
                collectionBean = new NewBean();
                collectionBean.setTime(System.currentTimeMillis());
                collectionBean.setNickName(giftUser.getUsername());
                collectionBean.setCover_image_url(ivUrl);
                collectionBean.setName(name);
                collectionBean.setPrice(price);
                collectionBean.setFavorites_count(counts);
                dbTool.insertCollectionBean(collectionBean);
                collectionBean.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                   if(e == null){
                       Toast.makeText(HotSecondActivity.this, "成功", Toast.LENGTH_SHORT).show();
                   }else {
                       Toast.makeText(HotSecondActivity.this, "失败", Toast.LENGTH_SHORT).show();
                   }
                    }
                });
            }else {
                Intent intent  = new Intent(HotSecondActivity.this , LogInActivity.class);
                startActivity(intent);
            }
            click = true ;
        }

    }
}
